#include "mainWindow.h"
#include "spdlog/spdlog.h"
#include "videoWidget.h"

using namespace std;

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent) {
    layout_ = new QHBoxLayout(this);
    this->setCentralWidget(new QWidget);
    this->centralWidget()->setLayout(layout_);
}

MainWindow::~MainWindow() {
}

void MainWindow::videoWidget(const char* videoPath) {
    int width = 0, height = 0;
    if (widgetNum_ >= 2) {
        return;
    }

    VideoWidget* vWidget = new VideoWidget(this);
    vWidget->open(videoPath);
    layout_->addWidget(vWidget);
    videoWidget_[widgetNum_] = vWidget;
    widgetNum_ += 1;

    switch (widgetNum_) {
        case 1:
            width = vWidget->videoWidth;
            height = vWidget->videoHeight;
            break;
        case 2:
            width = 1536;
            height = 432;
            break;

        default:
            width = 1536;
            height = 864;
            break;
    }
    resize(width, height);
}

void MainWindow::closeEvent(QCloseEvent* event) {
    SPDLOG_INFO("{}", __func__);
    for (int i = 0; i < widgetNum_; ++i) {
        VideoWidget* w = videoWidget_[i];
        if (w) {
            w->stop();
            delete w;
            w = nullptr;
        }
    }
}